package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dto.WarehouseAreaLocaDTO;
import com.itheima.mapper.WarehouseAreaLocaMapper;
import com.itheima.mapper.WarehouseAreaMapper;
import com.itheima.mapper.WarehouseMapper;
import com.itheima.pojo.WarehouseAreaLoca;
import com.itheima.pojo.WarehouseArea;
import com.itheima.pojo.Warehouse;
import com.itheima.service.WarehouseAreaLocaService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service

public class WarehouseAreaLocaServiceImpl extends ServiceImpl<WarehouseAreaLocaMapper, WarehouseAreaLoca> implements WarehouseAreaLocaService {

    @Autowired
    private WarehouseAreaMapper warehouseAreaMapper;

    @Autowired
    private WarehouseMapper warehouseMapper;

    // ... existing code ...
    @Override
    public Page<WarehouseAreaLocaDTO> getWarehouseAreaLocaPage(Page<WarehouseAreaLocaDTO> page, WarehouseAreaLocaDTO dto) {
        // 使用PageHelper进行分页
        PageHelper.startPage((int)page.getCurrent(), (int)page.getSize());

        // 构造查询条件
        LambdaQueryWrapper<WarehouseAreaLoca> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WarehouseAreaLoca::getIsDel, 1); // 只查询未删除的

        if (dto != null) {
            if (dto.getLocaCode() != null && !dto.getLocaCode().isEmpty()) {
                wrapper.like(WarehouseAreaLoca::getLocaCode, dto.getLocaCode());
            }
            if (dto.getWarehouseId() != null) {
                wrapper.eq(WarehouseAreaLoca::getWarehouseId, dto.getWarehouseId());
            }
            if (dto.getAreaId() != null) {
                wrapper.eq(WarehouseAreaLoca::getAreaId, dto.getAreaId());
            }
            if (dto.getIsValid() != null) {
                wrapper.eq(WarehouseAreaLoca::getIsDel, dto.getIsValid());
            }
        }

        // 执行查询
        List<WarehouseAreaLoca> locaList = this.list(wrapper);
        PageInfo<WarehouseAreaLoca> pageInfo = new PageInfo<>(locaList);

        Page<WarehouseAreaLocaDTO> dtoPage = new Page<>(page.getCurrent(), page.getSize(), pageInfo.getTotal());
        List<WarehouseAreaLocaDTO> records = convertToDtoList(locaList);

        // 联查 和 tb_warehouse 通过 area_id 从 tb_warehouse_area 联查 tb_warehouse_area.name 和 tb_warehouse_area.category,
        // 通过 warehouse_id 从 tb_warehouse 联查 tb_warehouse.name
        // 将联查信息添加到dto中

        // 获取所有关联的areaId和warehouseId
        List<Integer> areaIds = records.stream()
                .map(WarehouseAreaLocaDTO::getAreaId)
                .filter(id -> id != null)
                .distinct()
                .collect(Collectors.toList());

        List<Integer> warehouseIds = records.stream()
                .map(WarehouseAreaLocaDTO::getWarehouseId)
                .filter(id -> id != null)
                .distinct()
                .collect(Collectors.toList());

        // 批量查询关联信息
        Map<Integer, String> areaNameMap = null;
        Map<Integer, String> areaCategoryMap = null;
        if (!areaIds.isEmpty()) {
            List<WarehouseArea> areas = warehouseAreaMapper.selectBatchIds(areaIds);
            areaNameMap = areas.stream().collect(Collectors.toMap(
                    WarehouseArea::getId,
                    WarehouseArea::getName));
            areaCategoryMap = areas.stream().collect(Collectors.toMap(
                    WarehouseArea::getId,
                    WarehouseArea::getCategory));
        }

        Map<Integer, String> warehouseNameMap = null;
        if (!warehouseIds.isEmpty()) {
            List<Warehouse> warehouses = warehouseMapper.selectBatchIds(warehouseIds);
            warehouseNameMap = warehouses.stream().collect(Collectors.toMap(
                    Warehouse::getId,
                    Warehouse::getName));
        }

        // 填充关联信息到DTO
        for (WarehouseAreaLocaDTO record : records) {
            if (record.getAreaId() != null && areaNameMap != null) {
                record.setAreaName(areaNameMap.get(record.getAreaId()));
                record.setCategory(areaCategoryMap.get(record.getAreaId()));
            }
            if (record.getWarehouseId() != null && warehouseNameMap != null) {
                record.setWarehouseName(warehouseNameMap.get(record.getWarehouseId()));
            }
        }

        dtoPage.setRecords(records);
        return dtoPage;
    }


    @Override
    public WarehouseAreaLoca getWarehouseAreaLocaById(Integer id) {
        LambdaQueryWrapper<WarehouseAreaLoca> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WarehouseAreaLoca::getId, id)
                .eq(WarehouseAreaLoca::getIsDel, 1);
        return this.getOne(wrapper);
    }

    @Override
    public boolean saveWarehouseAreaLoca(WarehouseAreaLoca warehouseAreaLoca) {
        if (warehouseAreaLoca.getId() == null) {
            warehouseAreaLoca.setCreateTime(java.time.LocalDateTime.now());
            return this.save(warehouseAreaLoca);
        } else {
            warehouseAreaLoca.setUpdateTime(java.time.LocalDateTime.now());
            return this.updateById(warehouseAreaLoca);
        }
    }

    @Override
    public boolean updateWarehouseAreaLoca(WarehouseAreaLoca warehouseAreaLoca) {
        warehouseAreaLoca.setUpdateTime(java.time.LocalDateTime.now());
        return this.updateById(warehouseAreaLoca);
    }

    @Override
    public boolean deleteWarehouseAreaLoca(Integer id) {
        WarehouseAreaLoca warehouseAreaLoca = new WarehouseAreaLoca();
        warehouseAreaLoca.setId(id);
        warehouseAreaLoca.setIsDel(2); // 标记为已删除
        return this.updateById(warehouseAreaLoca);
    }

    private WarehouseAreaLocaDTO convertToDto(WarehouseAreaLoca warehouseAreaLoca) {
        WarehouseAreaLocaDTO dto = new WarehouseAreaLocaDTO();
        BeanUtils.copyProperties(warehouseAreaLoca, dto);
        dto.setIsValid(warehouseAreaLoca.getIsDel());
        return dto;
    }

    private java.util.List<WarehouseAreaLocaDTO> convertToDtoList(java.util.List<WarehouseAreaLoca> warehouseAreaLocas) {
        return warehouseAreaLocas.stream().map(this::convertToDto).toList();
    }
}
